<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Api\Controller;
use Admin\Model\UserModel;
use Admin\Model\RoleModel;
use Admin\Model\IntegralModel;
/**
 * Description of BaodanController
 *
 * @author Administrator
 */
class BaodanController extends ApiController {
    //put your code here
    private $userModel;
    private $roleModel;
    private $integralModel;
    function __construct() {
        parent::__construct();
        $this->userModel = new UserModel();
        $this->roleModel = new RoleModel();
        $this->integralModel = new IntegralModel();
    }
    public function save(){
        
        $userId = I('user_id');  //推荐人Id
        $integral = I('money');  //推荐人报单消耗积分
        $mobile = I('mobile');   //报单的手机号
        $real_name = I('real_name');  //报单姓名
        $fee =  $integral*0.1 < 1 ? 1 : $integral*0.1;
        
        //扣除积分
        $this->userModel->deductIntegral($userId,$integral + $fee);
        //扣除积分 日志
        $this->integralModel->add([
            'uid'=>$userId,
            'integral' => $integral + $fee,
            'ctime' => time(),
            'status' => 2,
            'mark' => '报单扣除:'.$integral.',服务费:'.$fee,
            'type' => 1
        ]);
        //根据配置增加代返还积分
        
        //判断推荐人是否存在 不存在新增用户
        $mobile_uinfo = $this->userModel->getUserInfoByMobile($mobile);
        if(empty($mobile_uinfo)){
            //新增用户
            $role = $this->roleModel->getRoleForRegister();
            $data = array(
    		'user_name' => $mobile,
    		'password' => md5(123456),
    		'mobile' => $mobile,
    		'create_time' => time(),
                'real_name' => $real_name,
    		'status' => 1,
    		'role_id' => $role['id'],
                'referee_id' => $userId
            );
            $rs = $this->userModel->add($data);
        }
		$uuid = $mobile_uinfo['id'] ? $mobile_uinfo['id'] : $rs;
        M('baodan')->add([
            'uid'=>$userId,
            'integral'=>$integral,
            'return_integral'=> C('multiple')*$integral,
            'ctime'=>time(),
            'mobile'=>$mobile,
            'real_name'=>$real_name,
            'uuid'=>$uuid
        ]);
		$this->userModel->increaseReturnIntegral($uuid,$integral*C('multiple'));
        //待返还积分增加日志
        $this->integralModel->add([
            'uid'=>$uuid,
            'integral' => $integral*C('multiple'),
            'ctime' => time(),
            'status' => 1,
            'mark' => '报单奖励'.$integral*C('multiple'),
            'type' => 2
        ]);
        return $this->callBack("报单提交成功",$this->SUCCESS_CODE,['integral'=>$integral,'return_integral'=>C('multiple')*$integral]);
    }
    
    public function getList(){
        $userId = I('user_id');  //推荐人Id
        $list = M('baodan')->where(['uid'=>$userId])->order("ctime desc")->select();
        foreach($list as $k=>$v){
            $list[$k]['str_time'] = Date('m-d',$v['ctime']);
            $list[$k]['uid_name'] = getUserName($v['uid']);
            $list[$k]['uuid_name'] = getUserName($v['uuid']);
        }
        return $this->callBack("报单列表",$this->SUCCESS_CODE,$list);
    }
	
	public function integral_list(){
		$userId = I("request.user_id");
		$list = $this->integralModel->getList(['uid'=>$userId],false);
		$this->assign("data",$list);
		return $this->callBack("用户积分列表",$this->SUCCESS_CODE,$list);
	}
}
